package com.koron.device.service.propconfig;

import com.koron.device.bean.Equipment;
import com.koron.device.bean.EquipmentQuery;
import com.koron.device.mapper.EquipmentMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author twcao
 * @title: ManagerUnitFilterConfig
 * @projectName GDH_EAM_V2
 * @description: 传入设备管理单位，对不是指定管理单位的设备做特殊标记，标记属性：filterManagerUnit
 * @date 2022/2/1520:19
 */
@Component
public class ManagerUnitFilterConfig implements PropConfig {

    @Override
    public List<String> sign() {
        return Arrays.asList("filterManagerUnit");
    }

    @Override
    public void setup(SessionFactory factory, List<Equipment> equipments, EquipmentQuery query) {
        if(CollectionUtils.isEmpty(query.getManagerUnits())) {
            // 如果没有传入设备管理单位，不做查询
            return;
        }
        EquipmentMapper mapper = factory.getMapper(EquipmentMapper.class);
        List<String> paramIds = mapper.countParam(equipments.stream().map(Equipment::getId).collect(Collectors.toList()), null, query.getManagerUnits());
        // 如果不存在于列表中，则不存在
        equipments.stream().forEach(row -> row.setFilterManagerUnit(paramIds.contains(row.getId())));
    }

    /**
     * 序号小的先执行
     *
     * @return
     */
    @Override
    public int getOrder() {
        return 2;
    }
}
